package cn.kgc.controller;

import cn.kgc.base.SystemConstant;
import cn.kgc.excpetion.GraceException;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.grace.result.ResponseStatusEnum;
import cn.kgc.util.FileManager;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author YC
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件管理模块")
public class FileController {

    @Resource
    private FileManager fileManager;

    @PostMapping("/upload/head")
    public GraceJSONResult uploadHeadForResume(@RequestParam("file") MultipartFile file) {
        // 判断文件是否为空
        if (file.isEmpty()) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        // 上传文件
        try {
            String filePath = fileManager.uploadFile(file, SystemConstant.FACE_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAIL);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/license")
    public GraceJSONResult uploadLicense(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath = fileManager.uploadFile(file, SystemConstant.LICENSE_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAIL);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/authLetter")
    public GraceJSONResult uploadAuthLetter(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath = fileManager.uploadFile(file, SystemConstant.AUTH_LETTER_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAIL);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/chatImage")
    public GraceJSONResult uploadImageForChat(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath = fileManager.uploadFile(file, SystemConstant.CHAT_IMAGE_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAIL);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/chatVideo")
    public GraceJSONResult uploadVideoForChat(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath = fileManager.uploadFile(file, SystemConstant.CHAT_VIDEO_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAIL);
            return GraceJSONResult.error();
        }
    }

    @PostMapping("/upload/chatAudio")
    public GraceJSONResult uploadAudioForChat(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        try {
            String filePath = fileManager.uploadFile(file, SystemConstant.CHAT_AUDIO_PATH);
            return GraceJSONResult.ok(filePath);
        } catch (Exception e) {
            GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAIL);
            return GraceJSONResult.error();
        }
    }

}
